package setting

import (
	"example.com/dcache/logger"
	"github.com/go-ini/ini"
)

//App settings
type App struct {
	AppName      string
	HashReplicas int64
}

var AppSetting = &App{}

//Server settings
type Server struct {
	RunMode      string
	Host         string
	Port         string
	UseRpc       int64
	RpcPort      int64
	ReadTimeout  int64
	WriteTimeout int64
	NameServer   string
}

var ServerSetting = &Server{}

//Database settings
type Database struct {
	Size int64
}

var DatabaseSetting = &Database{}

var cfg *ini.File

// Setup initialize the configuration instance
func Setup() {
	var err error
	cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		logger.Fatal("setting.Setup, fail to parse 'conf/app.ini': %v", err)
	}

	mapTo("server", ServerSetting)
	mapTo("app", AppSetting)
	mapTo("database", DatabaseSetting)

}

// mapTo map section
func mapTo(section string, v interface{}) {
	err := cfg.Section(section).MapTo(v)
	if err != nil {
		logger.Fatal("Cfg.MapTo %s err: %v", section, err)
	}
}
